﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using OpenCvSharp;
using Window = CPF.Controls.Window;

namespace DesktopCam
{
    public class Window1 : Window
    {
        protected override void InitializeComponent()
        {
            LoadStyleFile("res://DesktopCam/Stylesheet1.css");
            //加载样式文件，文件需要设置为内嵌资源

            Title = "摄像头Sample " + Program.Version;
            Width = 467;
            Height = 410;
            Background = null;
            Children.Add(new WindowFrame(this, new Panel
            {
                Width = "100%",
                Height = "100%",
                Children =
                {

                    new Button
                    {
                        Name ="openCameraButton",
                        MarginLeft = 48,
                        MarginTop = 23,
                        Content="打开摄像头",
                        Commands =
                        {
                            {
                                nameof(Button.Click),
                                (a,b)=>OpenCamera()
                            }
                        },
                        Visibility = Visibility.Hidden,
                    },
                    new Button
                    {
                        Name ="closeCameraButton",
                        MarginLeft = 153,
                        MarginTop = 23,
                        Content="关闭摄像头",
                        Commands =
                        {
                            {
                                nameof(Button.Click),
                                (a,b)=>CloseCamera()
                            }
                        },
                        Visibility = Visibility.Hidden,
                    },
                    new Button
                    {
                        Name ="closeCameraButton2",
                        MarginLeft = 230,
                        MarginTop = 23,
                        Content="关闭摄像头2",
                        Commands =
                        {
                            {
                                nameof(Button.Click),
                                (a,b)=>CloseCamera2()
                            }
                        },
                        Visibility = Visibility.Hidden,
                    },
                    new Button
                    {
                        Name ="openCameraButton3",
                        MarginLeft = 48,
                        MarginTop = 43,
                        Content="打开摄像头3",
                        Commands =
                        {
                            {
                                nameof(Button.Click),
                                (a,b)=>WampFunc.OpenCam()
                            }
                        }
                    },
                    new Button
                    {
                        Name ="closeCameraButton3",
                        MarginLeft = 153,
                        MarginTop = 43,
                        Content="关闭摄像头3",
                        Commands =
                        {
                            {
                                nameof(Button.Click),
                                (a,b)=>WampFunc.CloseCam()
                            }
                        }
                    },
                    new Picture
                    {
                        Name="cameraPicture",
                        PresenterFor = this,
                        Source="https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top-e3b63a0b1b.png",
                        MarginLeft = 48,
                        MarginTop = 62,
                        Height = 298,
                        Width = 339,
                    },
                    new TextBox
                    {
                        WordWarp = true,
                        PresenterFor = this,
                        Name = "logout",
                        MarginLeft = 418,
                        MarginTop = 34,
                        Height = 316,
                        Width = 404,
                        Visibility = Visibility.Hidden,
                    },
                }
            }));
            if (!DesignMode)//设计模式下不执行，也可以用#if !DesignMode
            {

            }
        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override async void OnInitialized()
        {
            base.OnInitialized();
            WampFunc.InitWamp().ContinueWith(t =>
            {
                WampFunc.SubscribeOpenCam(x => OnCamOpen(x));
                WampFunc.SubscribeCloseCam(s => OnCamClose(s));
            });
        }
        //用户代码
        public static CancellationTokenSource tokenSource;
        public void OpenCamera()
        {
            Picture cameraPicture = FindPresenterByName<Picture>("cameraPicture");
            var logout = FindPresenterByName<TextBox>("logout");
            logout.Text = "opening...";
            tokenSource = new CancellationTokenSource();
            OpenCVFunc.LogOut(logout, tokenSource.Token);
            OpenCVFunc.OpenCam(cameraPicture, tokenSource.Token);
        }

        public void CloseCamera()
        {
            var logout = FindPresenterByName<TextBox>("logout");
            logout.Text = "closed";
            tokenSource.Cancel();

        }

        public void CloseCamera2()
        {
            OpenCVFunc.CloseCam();
        }

        public void OnCamOpen(byte[] x)
        {
            Picture cameraPicture = FindPresenterByName<Picture>("cameraPicture");
            cameraPicture.Source = x;
            //var logout = FindPresenterByName<TextBox>("logout");
            //logout.Invoke(() => logout.AppentText("."));
        }

        public void OnCamClose(string s)
        {
            Picture cameraPicture = FindPresenterByName<Picture>("cameraPicture");
            cameraPicture.Source = "https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo_top-e3b63a0b1b.png";
            //var logout = FindPresenterByName<TextBox>("logout");
            //logout.Invoke(() => logout.Text = s);
            //Console.WriteLine(s);
        }

#endif
    }
}
